package com.example.adms.sa_token;

import cn.dev33.satoken.stp.StpInterface;
import com.alibaba.fastjson.JSONObject;
import com.example.adms.role.mapper.RoleMapper;
import com.example.adms.role.mapper.RolePermissionMapper;
import com.example.adms.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 获取权限
 */
@Component
public class PermCheck implements StpInterface {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String s = String.valueOf(loginId);
        User user = JSONObject.parseObject(s, User.class);
        return rolePermissionMapper.selectPermsByUserId(user.getId());
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String s = String.valueOf(loginId);
        User user = JSONObject.parseObject(s, User.class);
        return roleMapper.selectRolesById(user.getId());
    }
}
